﻿using System;
using System.Collections.Specialized;
using Peter.Common.Dialog;
using Peter.Common.Interfaces;
using Peter.Common.Services;
using Peter.Sdw.Data;

namespace Peter.Sdw.Logic.Dialogs
{
   /// <summary>
   /// Dialog used to log into SDW.
   /// </summary>
   public class LoginModel : DialogBase
   {
      private string m_Email;
      private string m_Password;
      private bool m_SaveLoginData;
      private readonly SdwData m_OptionData;

      /// <summary>
      /// Initializes a new add book view model.
      /// </summary>
      public LoginModel ()
      {
         this.Width = 400;
         this.Height = 250;
         this.Title = "Login";
         this.ShowOkButton = false;
         var peterOptions = ServiceManager.Get<IPeterOptions> ();
         this.m_OptionData = (SdwData)peterOptions.GetOptionData (SdwData.NAME);
         this.Email = this.m_OptionData.UserName;
         this.Password = this.m_OptionData.Password;
         this.SaveLoginData = this.m_OptionData.SaveLogin;
      }

      /// <summary>
      /// Gets or Sets the user's email.
      /// </summary>
      public string Email
      {
         get { return this.m_Email; }
         set
         {
            this.m_Email = value;
            this.RaisePropertyChanged("Email");
         }
      }

      /// <summary>
      /// Gets or Sets the user's Password.
      /// </summary>
      public string Password
      {
         get { return this.m_Password; }
         set
         {
            this.m_Password = value;
            this.RaisePropertyChanged ("Password");
         }
      }

      /// <summary>
      /// Gets or Sets if the login data should be saved or not.
      /// </summary>
      public bool SaveLoginData
      {
         get { return m_SaveLoginData; }
         set
         {
            this.m_SaveLoginData = value;
            this.RaisePropertyChanged ("SaveLoginData");
         }
      }

      /// <summary>
      /// Occurs when the ok button on the dialog is pressed.
      /// </summary>
      public override void OkExecuted (object parameter)
      {
         this.ShowOkButton = false;
         var values = new NameValueCollection
         {
            {"UserEmail", this.Email},
            {"Password", this.Password},
            {"__RequestVerificationToken", WebQueue.Instance.RequestToken}
         };
         WebQueue.Instance.Post (Constants.URL_LOGIN_REQUEST, values);
         this.m_OptionData.SaveLogin = this.SaveLoginData;
         if (this.SaveLoginData)
         {
            this.m_OptionData.UserName = this.Email;
            this.m_OptionData.Password = this.Password;
         }
         base.OkExecuted(parameter);
      }

      /// <summary>
      /// Gets the login token.
      /// </summary>
      public void GetToken ()
      {
         WebQueue.Instance.WebGetComplete += this.OnGetCompleted;
         WebQueue.Instance.Get (Constants.URL_GET_TOKEN);
      }

      /// <summary>
      /// Occurs when a web get request is completed.
      /// </summary>
      /// <param name="sender">WebQueue.</param>
      /// <param name="e">WebRequestEventArgs</param>
      private void OnGetCompleted (object sender, WebRequestEventArgs e)
      {
         try
         {
            if (e.Url == Constants.URL_GET_TOKEN)
            {
               WebQueue.Instance.WebGetComplete -= this.OnGetCompleted;
               if (!string.IsNullOrEmpty (WebQueue.Instance.RequestToken))
                  this.ShowOkButton = true;
            }
         }
         catch (Exception ex)
         {
            Console.Error.WriteLine (e.Response + "\n" + ex.Message);
         }
      }
   }
}
